<?php
/**
 * @copyright   Copyright (c) 2017 https://www.sapixx.com All rights reserved.
 * @license     Licensed (http://www.apache.org/licenses/LICENSE-2.0).
 * @author      pillar<ltmn@qq.com>
 * 行业分类
 */
namespace app\allwin\controller;
use app\allwin\model\AllwinEcardOrder;
use app\allwin\model\AllwinEcardUser;
use think\facade\Request;
use think\helper\Time;

class EcardUser extends Common{

    public function initialize() {
        parent::initialize();
        $this->assign('pathMaps', [['name'=>'账号余额','url'=>url("ecard_user/index")]]);
    }

    /**
     * 列表
     */
    public function index(int $types = 0){
        $keyword     = $this->request->param('keyword');
        $condition = [];
        $condition[] = ['AllwinEcardUser.member_miniapp_id','=',$this->member_miniapp_id];
        $condition[] = ['is_del', '=',$types];
        $time      = Request::param('time/d',0);
        $starttime = Request::param('starttime/s');
        $endtime   = Request::param('endtime/s');
        if ($time) {
            switch ($time) {
                case 2:
                    list($start, $end) = Time::yesterday();
                    break;
                case 30:
                    list($start, $end) = Time::month();
                    break;
                case 60:
                    list($start, $end) = Time::lastMonth();
                    break;
                default:
                    list($start, $end) = Time::today();
                    break;
            }
            $condition[] = ['AllwinEcardUser.create_time', '>=', $start];
            $condition[] = ['AllwinEcardUser.create_time', '<=', $end];
        } else {
            if ($starttime) {
                $condition[] = ['AllwinEcardUser.create_time', '>=', strtotime($starttime)];
            }
            if ($endtime) {
                $condition[] = ['AllwinEcardUser.create_time', '<=', strtotime($endtime)];
            }
        }
        $view['lists'] = AllwinEcardUser::hasWhere('user', function($query) use($keyword) {
            if($keyword){
                $query->where('phone_uid|nickname', 'like', '%'.$keyword.'%', 'or');
            }
        })->where($condition)->order('id desc')->paginate(20);
        $view['normal']    = AllwinEcardUser::where($this->mini_program)->where(['is_del' => 0])->count();
        $view['delete']    = AllwinEcardUser::where($this->mini_program)->where(['is_del' => 1])->count();
        $view['invalid']   = AllwinEcardUser::where($this->mini_program)->where(['is_del' => 2])->count();
        $view['types']     = $types;
        $view['time']      = $time;
        $view['starttime'] = $starttime;
        $view['endtime']   = $endtime;
        $view['keyword']   = $keyword;
        return view()->assign($view);
    }

    //订单列表
    public function order(){
        $uid     = $this->request->param('uid');
        $view['lists'] = AllwinEcardOrder::where(['member_miniapp_id' => $this->member_miniapp_id,'uid' => $uid])->order('id desc')->paginate(20);
        return view()->assign($view);
    }
}